import { http } from "@/utils/http";
import type { RootThunkAction } from "@/types/store";
import type { User, UserPhotoResponse, UserProfile } from "@/types/data";
import type { UserResponse } from "@/types/data";
import type { UserProfileResponse } from "@/types/data";
import { getToken } from "@/utils/token";
// 我的页面 - 获取个人信息
export const getUser = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<UserResponse>("/user");

    const { data, message } = res.data;

    // 分发action 将用户信息存入redux中
    dispatch({ type: "profile/getUser", payload: data });
  };
};

// 我的页面 - 个人资料获取信息

export const getUserProfile = (): RootThunkAction => {
  return async (dispatch) => {
  
      const res = await http.get<UserProfileResponse>("/user/profile");
      dispatch({ type: "profile/getUserProfile", payload: res.data.data });

  };
};

export const updateUserProfile = (
  userProfile: Partial<UserProfile>
): RootThunkAction => {
  return async (dispatch) => {
    await http.patch("/user/profile", userProfile);
    // 分发 action 更新用户昵称
    dispatch({ type: "profile/update", payload: userProfile });
  };
};

export const updateUserPhoto = (data: FormData): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.patch<UserPhotoResponse>("/user/photo", data);
    dispatch({ type: "profile/update", payload: { photo: res.data.photo } });
  };
};
